package process

import (
	"errors"
	"os"
	"os/exec"
	"path/filepath"
	"pm/model"
	"time"
)

func (p *Process2)TryStart(process *model.Process) error{

	cmd := exec.Command(process.ProcessFile, process.Args)
	logOutput := NewLogOutput(process.Log, InfoLogType, process)
	logErrorOutput := NewLogOutput(process.ErrorLog, ErrorLogType, process)
	cmd.Stdin = os.Stdin
	cmd.Stderr = logErrorOutput
	cmd.Stdout = logOutput
	cmd.Dir = filepath.Dir(process.ProcessFile)
	if err := cmd.Start();err != nil{
		return errors.New("进程开启失败-"+err.Error())
	}
	process.Pid = cmd.Process.Pid
	process.Pointer = cmd
	process.Date = time.Now().Unix()
	go p.listenerExit(process)
	return nil
}
